//
//  THJStoreManageVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/11/24.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJStoreManageVC.h"
#import "THJStoreMngerTitleCell.h"
#import "THJCompanyIntroVC.h"
#import "THJEditComapnInfoVC.h"
#import "THJProductManageVC.h"
#import "THJPostZiZhiVC.h"

@interface THJStoreManageVC ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImage * img;
    UITableView * _tableview;
}
@end

@implementation THJStoreManageVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setGoBackBtn];
    self.navigationController.navigationBar.hidden = YES;
    [self setNavBarWithTitle:@"店铺管理"];
   _tableview = [self setTableviewWithFrame:CGRectMake(0, -20, ScreenWidth, ScreenHeight + 20) RegisterNibNameArr:@[@"THJStoreMngerTitleCell"] ReuseIdentifierArr:@[@"title"] RowHeight:50 BgColor:SingleColor(whiteColor) IsLoadMore:NO];
    [self setGoBackBtn];
    // Do any additional setup after loading the view from its nib.
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        if (indexPath.row == 0) {
            return 200;
        }else{
            return 44;
        }
    }else{
        return 44;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 2;
    }else{
        return 3;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            THJStoreMngerTitleCell * cell = [tableView dequeueReusableCellWithIdentifier:@"title" forIndexPath:indexPath];
            [cell.changImgBtn addTarget:self action:@selector(changeBackImg) forControlEvents:UIControlEventTouchUpInside];
            
            
            [cell.imgV sd_setImageWithURL:[NSURL URLWithString:self.cInfoDic[@"banner"]] placeholderImage:kGetImage(@"headBgPic")];
            
            return cell;
        }else{
            UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            if(!cell){
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            }
            cell.textLabel.text = @"完善企业信息";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cell;
        }
    }else{
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        if(!cell){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
        }
        NSArray * titleArr2 = @[@"完善企业简介",@"上传资质",@"产品管理"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = titleArr2[indexPath.row];
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 0){
        if(indexPath.row == 1){
            [self.navigationController pushViewController:[THJEditComapnInfoVC new] animated:YES];
        }
    }else{
        if(indexPath.row == 0){
             [self.navigationController pushViewController:[THJCompanyIntroVC new] animated:YES];
        }else if (indexPath.row == 2){
            [self.navigationController pushViewController:[THJProductManageVC new] animated:YES];
        }else if (indexPath.row == 1){
            [self.navigationController pushViewController:[THJPostZiZhiVC new] animated:YES];
        }
    }
}
-(void)changeBackImg{
    [self alertWithTitle1:@"取消" title2:@"拍照" title3:@"相册"];
}
#pragma mark ----更多操作---
-(void)actionSheetIndex0Function{
    NSLog(@"取消1");
}
-(void)actionSheetIndex1Function{
    [self getPhotoWithType:0];
}
-(void)actionSheetIndex2Function{
    [self getPhotoWithType:1];
}

/**
 调用相册或相机
 */
-(void)getPhotoWithType:(NSInteger)index{
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (index) {
            case 0:
                //来源:相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1:
                //来源:相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 2:
                return;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.navigationBar.translucent = NO;
    imagePickerController.sourceType = sourceType;
    
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    img = image;
    [_tableview reloadData];
    [self postImgData];
}
-(void)postImgData{
    NSString * token = [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
    NSString * imgData = [self image2DataURL:img];
    NSString * url = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=member&r=member&m=banner"];
    [THJNetWork postDataFromUrl:url parameters:@{@"banner":imgData} Token:token callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"上传图片-%@",receiveData);
        [self.cInfoDic setValue:receiveData[@"data"] forKey:@"banner"];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
            [_tableview reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
            
        });

        
    }];
}
@end
